<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @cancel="handleCancel"
    :body-style="{
      padding: '10px'
    }"
    cancelText="关闭"
  >
    <template #footer>
      <a-button type="danger" @click="handleCancel">取消</a-button>
      <a-button type="primary" v-if="currentStep !== 1" @click="prevStep">上一步</a-button>
      <a-button type="primary" v-if="currentStep !== totalSteps" @click="nextStep">下一步</a-button>
      <a-button type="primary" v-if="currentStep === totalSteps" @click="handleOk">生成请购单</a-button>
    </template>

    <!--    页面-->
    <div>
      <SelectMaterialTable v-show="currentStep === 1" ref="materialTable"></SelectMaterialTable>
      <MrpCalCList v-show="currentStep === 2" ref="mrpList"></MrpCalCList>
    </div>
  </j-modal>
</template>

<script>
import HfMaterialSearchSelectTag from '@comp/HfManual/HfMaterialSearchSelectTag.vue'
import SelectMaterialTable from '@views/purchase/MPRCalc/SelectMaterialTable.vue'
import MrpCalCList from '@views/purchase/MPRCalc/MrpCalCList.vue'
import { postAction } from '@api/manage'

export default {
  name: 'MPRCalcModal',
  components: { MrpCalCList, SelectMaterialTable, HfMaterialSearchSelectTag },
  computed: {
    title() {
      switch (this.currentStep) {
        case 1:
          return '1.请先选择物料'
        case 2:
          return '2.需要采购的物料清单'
        default:
          return ''
      }
    }
  },
  data() {
    return {
      width: 1400,
      visible: false,
      confirmLoading: false,
      currentStep: 1,
      totalSteps: 2,
      url: {
        list: '/production/hfRouting/stepSelectByProductInput',
        materialList: '/material/hfMaterial/queryByMaterialCodeOrName',
        generateRequest: '/purchase/hfPurchaseRequest/generateByMRP'
      }
    }
  },
  methods: {
    close() {
      this.$emit('close')
      this.visible = false
      this.currentStep = 1
    },
    handleCancel() {
      this.close()
    },
    handleOk() {
      this.$refs.mrpList.generateRequest()
    },
    prevStep() {
      if (this.currentStep <= this.totalSteps && this.currentStep > 1) {
        this.currentStep--
      }
    },
    nextStep() {
      if (this.$refs.materialTable.dataSource.length === 0) {
        this.$message.error('请先选择物料')
        return
      }
      if (this.currentStep < this.totalSteps) {
        this.currentStep++
      }
      if (this.currentStep === 2) {
        this.$refs.mrpList.loadData(this.$refs.materialTable.dataSource)
      }
    },
    onMaterialChange([value, record], tableRow) {
      // 初始化数据
      Object.assign(tableRow, {
        materialId: record.materialId,
        materialCode: record.materialCode,
        materialName: record.materialName,
        sap: record.sap,
        unit: record.unit,
        unit_dictText: record.unit_dictText,
        materialType: record.materialType,
        materialType_dictText: record.materialType_dictText,
        perNumber: record.perNumber,
        warehouse: record.warehouse,
        warehouse_dictText: record.warehouse_dictText,
        price: record.price,
        tax: record.tax,
        taxPrice: record.taxPrice,
        saleOutTotalPrice: 0.0,
        totalPrice: 0.0
      })
    }
  }
}
</script>

<style scoped></style>
